<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>会员卡列表</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  {include file="public/css"/}
</head>
<body>
  <div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-card layui-col-md12">
          <div class="layui-card-header">
						会员卡列表
						{if input('param.isopen')==1}<i class="layui-icon layui-icon-close" style="font-size:18px;font-weight:bold;cursor:pointer" onclick="closeself()"></i>{/if}
					</div>
          <div class="layui-card-body" pad15>
						<div class="layui-col-md3" style="padding-bottom:10px">
							<button class="layui-btn  layuiadmin-btn-list" onclick="openmax('{:url('edit')}/isopen/1')">创建会员卡</button>
						</div>
						<div class="layui-form layui-col-md9" style="text-align:right;padding-bottom:10px">
							
						</div>
						<div class="layui-col-md12">
							<table id="tabledata" lay-filter="tabledata"></table>
						</div>
          </div>
        </div>
    </div>
  </div>
	{include file="public/js"/}
	<script>
  var table = layui.table;
	var datawhere = {};
  //数据表
  var tableIns = table.render({
    elem: '#tabledata'
    ,url: '{$Request.url}' //数据接口
    ,page: true //开启分页
		,checkbox: true
    ,cols: [[ //表头
			{type:"checkbox"},
      {field: 'id', title: 'ID',sort: true,width:60},
      {field: 'card_id', title: '卡券ID',width:250},
      {field: 'brand_name', title: '商户名称'},
      {field: 'title', title: '会员卡名称'},
      {field: 'status', title: '状态',width:100,templet:function(d){
				if(d.status==0){
					return '<span style="color:blue">审核中</span>';
				}else if(d.status==1){
					return '<span style="color:green">已通过</span>';
				}else if(d.status==2){
					return '<span style="color:red">未通过</span>' + d.RefuseReason;
				}
				return '';
			}},
      {field: 'createtime', title: '创建时间',sort: true,templet:function(d){ return date('Y-m-d H:i',d.createtime)}},
      {field: 'membercount', title: '领取数量'},
      {field: 'remark', title: '备注'},
      {field: 'operation', title: '操作',templet: function(d){
				var html = '';
				html+='<button class="table-btn" onclick="openmax(\'{:url('edit')}/id/'+d.id+'/isopen/1\')">编辑</button>';
				html+='<button class="table-btn" onclick="datadel('+d.id+')">删除</button>';
				html+='<button class="table-btn" onclick="setremark('+d.id+')">备注</button><br>';
				if(d.ret_url){
					html+='<button class="table-btn" onclick="showreturl('+d.id+',\''+d.ret_url+'\')">开卡链接</button>';
					html+='<button class="table-btn" onclick="openmax(\'{:url('record')}/card_id/'+d.card_id+'/isopen/1\')">领取记录</button>';
				}
				
				
				return html;
			},width:300}
    ]]
  });
	//排序
	table.on('sort(tabledata)', function(obj){
		datawhere.field = obj.field;
		datawhere.order = obj.type;
		tableIns.reload({
			initSort: obj,
			where: datawhere
		});
	});
	//日期范围选择
	layui.laydate.render({ 
		elem: '#ctime',
		trigger: 'click',
		range: '~' //或 range: '~' 来自定义分割字符
	});
	//检索
	layui.form.on('submit(LAY-app-forumreply-search)', function(obj){
		var field = obj.field
		var olddatawhere = datawhere
		datawhere = field
		datawhere.field = olddatawhere.field
		datawhere.order = olddatawhere.order
		tableIns.reload({
			where: datawhere,
			page: {curr: 1}
		});
	})
	//删除
	function datadel(id){
		var ids = [];
		if(id==0){
			var checkStatus = table.checkStatus('tabledata')
			var checkData = checkStatus.data; //得到选中的数据
			if(checkData.length === 0){
				 return layer.msg('请选择数据');
			}
			var ids = [];
			for(var i=0;i<checkData.length;i++){
				ids.push(checkData[i]['id']);
			}
		}else{
			ids.push(id)
		}
		layer.confirm('确定要删除吗?',{icon: 7, title:'操作确认'}, function(index){
			//do something
			layer.close(index);
			var index = layer.load();
			$.post("{:url('del')}",{ids:ids},function(data){
				layer.close(index);
				dialog(data.msg,data.status);
				tableIns.reload()
			})
		});
	}
	function showreturl(id,url){
		layer.open({
			type:1,
			area:['350px','420px'],
			content:'<div style="margin:auto auto;text-align:center"><div style="margin:10px 30px" id="returl"></div><div style="font-size:12px;color:#666;word-break:break-all;padding:0 10px">'+url+'</div></div>',
			title:'开卡链接',
			shadeClose:true
		});
		var qrcode = new QRCode('returl', {
			text: 'your content',
			width: 280,
			height: 280,
			colorDark : '#000000',
			colorLight : '#ffffff',
			correctLevel : QRCode.CorrectLevel.L
		});
		qrcode.clear();
		qrcode.makeCode(url);
	}
	function setremark(id){
		var html = '';
		html+='	<div class="layui-form-item" style="margin-top:40px;margin-right:20px;">';
		html+='		<label class="layui-form-label" style="width:80px">备注信息：</label>';
		html+='		<div class="layui-input-inline" style="width:200px">';
		html+='			<input type="text" id="remark" class="layui-input"/>';
		html+='		</div>';
		html+='	</div>';
		var setstLayer = layer.open({type:1,area:['500px','300px'],title:false,content:html,shadeClose:true,btn: ['确定', '取消'],
			yes:function(){
				var index = layer.load();
				$.post("{:url('setremark')}",{id:id,remark:$('#remark').val()},function(res){
					layer.close(index);
					dialog(res.msg,res.status);
					layer.close(setstLayer);
					tableIns.reload()
				})
			}
		})

	}

	
	var pagenum = 0;
	var pagelimit = 20;
	function sendscore(id){
		pagenum = 0;

		var html = '';
		html+='	<div class="layui-form-item" style="margin-top:40px;margin-right:20px;">';
		html+='		<label class="layui-form-label" style="width:100px">推送积分数：</label>';
		html+='		<div class="layui-input-inline" style="width:200px">';
		html+='			<input type="number" id="sendscore_score" class="layui-input"/>';
		html+='		</div>';
		html+='	</div>';
		html+='	<div class="layui-form-item" style="margin-top:40px;margin-right:20px;">';
		html+='		<label class="layui-form-label" style="width:100px">备注信息：</label>';
		html+='		<div class="layui-input-inline" style="width:350px">';
		html+='			<textarea type="text" id="sendscore_remark" class="layui-textarea"></textarea>';
		html+='		</div>';
		html+='	</div>';
		var addscoreLayer = layer.open({type:1,area:['550px','360px'],title:false,content:html,shadeClose:true,btn: ['确定', '取消'],
			yes:function(){
				var score = $('#sendscore_score').val();
				var remark = $('#sendscore_remark').val();
				if(!score){
					dialog('请填写积分数');
					return;
				}
				layer.close(addscoreLayer);
				_scoresend({id:id,score:score,remark:remark});
			}
		})

		return;
	}
	function _scoresend(field){
			var index = layer.load();
			pagenum++;
			$.ajax({
				type:'POST',
				url:"{:url('sendscore')}/pagenum/"+pagenum+'/pagelimit/'+pagelimit,
				dataType:'json',
				data:field,
				success:function(res){
					console.log(res)
					layer.close(index);
						layer.msg('已发送'+res.sendcount+'条,成功'+res.successcount+'条,失败'+res.errorcount+'条',{offset:'100px'});
					if(res.status==2){ //继续发送
						field.logid = res.logid
						_scoresend(field);
					}else{
						if(res.status==1){
							dialog('共发送'+res.sendcount+'条,成功'+res.successcount+'条,失败'+res.errorcount+'条',res.status);
							pagenum = 0;
						}else{
							dialog(res.msg,res.status);
						}
					}
				},
				error:function(){
					layer.close(index);
					dialog('未知错误',0);
				}
			})
	}
	</script>
	{include file="public/copyright"/}
</body>
</html>